/**
 * 
 */
package fr.utbm.gi.vi51.slizers.pw.environment;

import java.util.LinkedList;
import java.util.List;

import javax.vecmath.Point2i;

import fr.utbm.gi.vi51.slizers.framework.agent.AgentBody;
import fr.utbm.gi.vi51.slizers.framework.agent.Direction;
import fr.utbm.gi.vi51.slizers.framework.environment.Perception;
import fr.utbm.gi.vi51.slizers.framework.environment.PerceptionGenerator;
import fr.utbm.gi.vi51.slizers.framework.environment.PerceptionPosition;

/**
 * Implementation of an perception generator for the world.
 * 
 * @author Tarik Belabbas
 * 
 */
public class WorldPerceptionGenerator extends PerceptionGenerator< World >
{
	/**
	 * Parameterized constructor.
	 */
	public WorldPerceptionGenerator( World _world )
	{
		super( _world );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.utbm.gi.vi51.slizers.framework.environment.PerceptionGenerator#
	 * computePerceptionsFor(fr.utbm.gi.vi51.slizers.framework.body.AgentBody)
	 */
	@Override
	public List< Perception > computePerceptionsFor( AgentBody _agentBody )
	{
		List< Perception > perceptions = new LinkedList< Perception >();
		
		World world = getEnvironment();
		Point2i position = new Point2i();
		WorldElement perceivedItem;
		Direction direction = _agentBody.getDirection();
		
		// DOWN
		position.set( _agentBody.getPosition() );
		perceivedItem	= (world.getRelativeWorldElement( position, RelativePosition.BELOW ) );
		perceptions.add(new Perception(perceivedItem, PerceptionPosition.DOWN));
		
		// FRONT
		position.set( _agentBody.getPosition() );
		if(direction == Direction.LEFT)
			perceivedItem = (world.getRelativeWorldElement( position, RelativePosition.LEFT ));
		else
			perceivedItem = (world.getRelativeWorldElement( position, RelativePosition.RIGHT ));
		perceptions.add(new Perception(perceivedItem, PerceptionPosition.FRONT));
		
		// UP
		position.set( _agentBody.getPosition() );
		perceivedItem	= (world.getRelativeWorldElement( position, RelativePosition.ABOVE ));
		perceptions.add(new Perception(perceivedItem, PerceptionPosition.UP));
		
		// FRONT_DOWN
		position.set( _agentBody.getPosition() );
		position.x		+= direction.dx;
		perceivedItem	= (world.getRelativeWorldElement( position, RelativePosition.BELOW ));
		perceptions.add(new Perception(perceivedItem, PerceptionPosition.FRONT_DOWN));
		
		// FRONT UP
		position.set( _agentBody.getPosition() );
		position.x		+= direction.dx;
		perceivedItem	= (world.getRelativeWorldElement( position, RelativePosition.ABOVE ));
		perceptions.add(new Perception(perceivedItem, PerceptionPosition.FRONT_UP));
		
		return perceptions;
	}
}
